import {
  IndexShowData,
  GetCatData,
  GetImageData,
  UserReturn,
  UserData,
  GetOption,
} from "@/api/interface";
import { get, gets, post } from "@/api/http";

//对Axios 进行简单的封装
/**
 *
 * @param p 配置信息
 * @returns 返回需要的信息
 */

//获取最新的文章基础数据，
/**
 * 配置信息：
 * 查询的文章数 - per_page: 11
 * 文章ID      - include: 1
 * 分类ID      - categories: 1
 */

/**
 * 获取文章信息
 * 配置信息：最新文章数：per_page: 11，页数：page:2
 * 示例链接：http://magick.plugin/wp-json/wp/v2/posts/?_fields=id,categories,title,link,date,featured_media&per_page=11
 * @param p 配置信息
 * @returns 数组
 */
export const apiGetIndexPostData = async (p: {}) => {
  const response = await get("wp-json/wp/v2/posts/?_fields=id", p);
  return response as any;
};

//定制接口，返回所有数据
export const apiGetIndexPostDatas = async (p: {}) => {
  const response = await gets("wp-json/wp/v2/posts/?_fields=id", p);
  return response as any;
};

/**
 * 获取指定文章ID的相关信息
 * 配置信息： include:1
 * 示例：http://magick.plugin/wp-json/wp/v2/posts/?_fields=id,categories,title,link,date,featured_media&&include=1
 * @param p 配置信息
 * @returns 数组
 */
export const apiGetPostData = async (p: {}) => {
  const response = await get("wp-json/wp/v2/posts/", p);
  return response as any;
};

/**
 * 获取图片信息
 * 配置：图片ID      - include: 1
 * 示例：http://magick.plugin//wp-json/wp/v2/media/2312?_fields=guid
 * @param p 配置信息
 * @returns 数组
 */
export const apiGetImgData = async (p: {}): Promise<GetImageData[]> => {
  const response = await get(
    "wp-json/wp/v2/media/?_fields=id,guid,caption,alt_text,media_details",
    p
  );
  return response as GetImageData[];
};

/**
 * 获取指定分类的信息
 * 配置：分类ID - include: 1
 * 示例链接：http://magick.plugin/wp-json/wp/v2/categories/1?_fields=id,count,name,link
 * @param p 配置信息
 * @returns 数组
 */
export const apiGetCatData = (p: {}) => {
  const response = get("wp-json/wp/v2/categories/", p);
  return response as any;
};

/**
 * 获取指定分类下的文章id，输入指定分类ID，
 * 配置：分类ID  - categories: 1
 * 示例链接：http://magick.plugin/wp-json/wp/v2/posts?categories=1&per_page=6&_fields=id,
 * @param p 配置信息
 * @returns 数组
 */
export const apiGetCatPostData = async (p: {}) => {
  const response = await get("wp-json/wp/v2/posts?categories?_fields=id", p);
  return response as Array<{ id: number }>;
};

/**
 * 获取搜索结果的ID
 * 配置：搜索内容:search: "世界",
 * 示例链接http://magick.plugin/wp-json/wp/v2/posts?_fields=id,categories,title,link,date,featured_media&search=世界
 * @param p 配置信息
 * @returns 数组
 */
export const apiGetSearchData = async (p: {}) => {
  const response = await get("wp-json/wp/v2/posts?_fields=id", p);
  return response as unknown as Array<{ id: number }>;
};

/**
 * 登录请求,提供账号密码，获取返回对象中的token
 * @param p 配置信息 username\password:
 * @returns 对象
 */
export const apiPostSign = async (p: {}): Promise<UserReturn> => {
  const response = await post("wp-json/jwt-auth/v1/token", p);
  return response as UserReturn;
};

/**
 * 获取当前登录用户信息
 * @param p 配置信息
 * @returns 对象
 */
export const apiGetUserData = async (p: {}): Promise<UserData> => {
  const response = await get("wp-json/wp/v2/users/me", p);
  return response as UserData;
};

/**
 * 修改用户信息
 * @param p 提供用户ID，提供修改信息
 * @returns 对象
 */
export const apiReviseUserData = async (id: number, p: {}) => {
  const response = await post("wp-json/wp/v2/users/" + id, p);
  return response as any;
};

/**
 * 修改文章信息
 * @param p 提供文章ID，提供修改信息
 * @returns 对象
 */
export const apiRevisePostData = async (id: number, p: {}) => {
  const response = await post("wp-json/wp/v2/posts/" + id, p);
  return response as any;
};

/**
 * 获取选项信息
 * @param p 配置信息
 * @returns 对象
 */
export const apiGetConfigData = async (p: {}): Promise<GetOption> => {
  const response = await get("wp-json/carbon-fields/v1/h5-options", p);
  return response as GetOption;
};

/**
 * 修改选项信息
 * @param p 修改信息
 * @returns 对象
 */
export const apiReviseConfigData = async (p: {}) => {
  const response = await post("wp-json/carbon-fields/v1/options", p);
  return response as any;
};

/**
 * 获取指定文章ID信息
 * //http://magick.plugin/wp-json/carbon-fields/v1/posts/2278
 */

export const apiGetPostIdData = async (p: {}, id: number) => {
  const response = await get(
    "wp-json/carbon-fields/v1/posts/" + id + "?_fields=id,date,title,image,cat",
    p
  );
  return response as any;
};
